<?php
class FunctionSession{
	static function login($email_address, $password){
		$rs = new Result();
		if(!Session::isLogin()){
			if(Session::login($email_address, $password)){
				$rs->return = true;
			}else{
				$rs->addError(14 ,"login fail");
			}
		}else{
		}
		return $rs;
	}
	static function getEmailAddress(){
		return Session::getEmailAddress();
	}
	static function isLogin(){
		return Session::isLogin();
	}
	static function loginGuest($guestid){
		return Session::guestLogin($guestid);
	}
	static function logout(){
		$rs = new Result();
		Session::logout();
		return $rs;
	}
	static function getCertificate($mac_address,$device_id){
		$rs = new Result();
		if(!Session::isLogin() || (Session::getAccountType() != 1 )){
			{Error::getInstance() ->addError( 17  , "Please login with an main account first!" .Session::getID().":". Session::getAccountType() );} ;
		}else{
			$email_address = FunctionData::getIdentifyFromId(Session::getID());
			$cers = FunctionData::readCertificates($email_address);
			$cer_secret = $cers->createCertificate($mac_address,$device_id);
			if($cer_secret != null){
				$account = FunctionData::readAccountRegisterInfo($email_address);
				$secret_code = $account->getSecretKey();
				if(strlen($secret_code)>0){
					$str = ( $mac_address  .  $secret_code  .  $cer_secret  . ((time()  - (time() % (30* 86400 )  ))/ (30* 86400 )  )) ;
					$certificate = UtilHash::sha256( $str ) ;
					$rs->addData('certificate',$certificate);
					$rs->return = true;
				}
				FunctionData::writeCertificates($email_address,$cers);
			}else{
				echo "mac in use";
				{Error::getInstance() ->addError( 18  , "This Mac Had been use." );} ;
			}	
		}
		return $rs;
	}
	static function checkCertificate($email_address,$mac_address,$cer){
		if(FunctionData::checkEmailRegisted($email_address)){
			$account = FunctionData::readAccountRegisterInfo($email_address);
			$secret_code = $account->getSecretKey();
			$cers = FunctionData::readCertificates($email_address);
			$cer_info = $cers->getCertificate($mac_address);
			if($cer_info != null){
				$cer_secret = $cer_info->getSecretCode();
				$str = ( $mac_address  .  $secret_code  .  $cer_secret  . ((time()  - (time() % (30* 86400 )  ))/ (30* 86400 )  )) ;
				$certificate = UtilHash::sha256( $str ) ;
				if($certificate == $cer) return true;
				else{
					echo "cer not valid:\n";
					echo "rev:".$cer."\n";
					echo "musbe:".$certificate."\n";
					echo "str:".$str.'\n';
					echo "". ((time()  - (time() % (30* 86400 ) ))/ (30* 86400 ) ) ."\n";
				}
			}else{
				{Error::getInstance() ->addError( 19  , "Invalid certificate!" );} ;
			}
		}else{
			{Error::getInstance() ->addError( 1  , "Invalid mail address!" );} ;
			echo "invalid email:".$email_address;
		}
		return false;
	}
	static function loginWithCertificate($identify,$device_id,$cer){
		$rs = new Result();
		if(self::checkCertificate($identify,$device_id,$cer)){
			$id = FunctionData::getIDFromIdentify($identify);
			if(Session::hardLogin($id)){
				$rs->addData("userid",Session::getID());
				$rs->return = true;
			}
		}
		return $rs;
	}
	static function loginWithFb($fbid){
		$id = FunctionData::getIdByFacebookAccount($fbid);
		if($id > -1){
			$email_address = FunctionData::getEmailAddressFromId($id);
			if($email_address != null){
				return Session::hardLogin($id);
			}else{
				echo "email for id ".$id."not found";
			}
		}else{
			echo "cannot found id of this fbid";
		}
		return false;
	}
	static function isFriend($fid){
		if(Session::isLogin()){
			$friends = FunctionData::readFriends(FunctionData::getIdentifyFromId(SESSION::getID()));
			return ($friends->isFriend($fid));
		}
		{Error::getInstance() ->addError( 17  , "Please login first" );} ;
		return false;
	}
	static function isFriendOfUserID($uid,$fid){
		if(FunctionData::checkIDValid($uid)){
			$friends = FunctionData::readFriends(FunctionData::getIdentifyFromId($uid));
			return ($friends->isFriend($fid));
		}
		{Error::getInstance() ->addError( 12  , "Account id invalid!" );} ;
		return false;
	}
	static function getFriends(){
		if(Session::isLogin()){
			$identify = Session::getIdentify();
			$friends = FunctionData::readFriends($identify);
			return $friends;
		}else{
			{Error::getInstance() ->addError( 17  , "Please login first" );} ;
		}
		return null;
	}
	static function addFriend($fid){
		if(FunctionData::checkIDValid($fid)){
			$friends = self::getFriends();
			if($friends != null){
				if($friends->addFriend($fid)){
					$identify = Session::getIdentify();
					FunctionData::writeFriends($identify,$friends);
					return true;
				}
			}
		}
		return false;
	}
	static function getFriendsOfUser($identify){
		if(FunctionData::checkIdentifyValid($identify)){
			$friends = FunctionData::readFriends($identify);
			return $friends;
		}
		return null;
	}
	static function addFriendToUser($identify,$id){
		if(FunctionData::checkIdentifyValid($identify) && FunctionData::checkIDValid($id)){
			$friends = FunctionData::readFriends($identify);
			if($friends != null && FunctionData::getEmailAddressFromId($id)!=null){
				$friends->addFriend($id);
				FunctionData::writeFriends($identify,$friends);
			}else{
				{Error::getInstance() ->addError( 0 , "Email address or ID not valid" );} ;
			}
		}
	}
}
?>